package com.infoland.afanmall.model.youXuan.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.model.youXuan.entity.ChangYongEntity;
import com.infoland.afanmall.model.youXuan.entity.FaXianEntity;
import com.infoland.afanmall.model.youXuan.entity.ZuiJinEntity;
import com.wyj.floatlayout.FlowLayout;
import com.wyj.floatlayout.TagAdapter;
import com.wyj.floatlayout.TagFlowLayout;
import com.wyj.floatlayout.TagView;

import java.util.ArrayList;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchActivity extends AppCompatActivity {

    @BindView(R.id.ys_title)
    LinearLayout ysTitle;
    //
    ArrayList<ZuiJinEntity> zuijinList;
    ArrayList<FaXianEntity> faxianList;
    ArrayList<ChangYongEntity> changyongList;
    //
    int spanCount = 6;
    @BindView(R.id.zj_flow_layout)
    TagFlowLayout zjFlowLayout;
    @BindView(R.id.fx_flow_layout)
    TagFlowLayout fxFlowLayout;
    @BindView(R.id.cy_flow_layout)
    TagFlowLayout cyFlowLayout;
    @BindView(R.id.ys_search)
    EditText ysSearch;

    private int getLayout() {
        return R.layout.search_activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        StatusBarKit.immersiveBlackTitleText(this, ysTitle);
        initData();
    }

    private void initData() {
        getZuiJin();
        getFaXian();
        getChangYong();
        ysSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//                ToastUtils.showShort(" true");
                closeKeyBoard();
                return true;
            }
//            ToastUtils.showShort(" false");
            return false;
        });
    }

    //获取发现
    public void getFaXian() {
        setFaXian();
    }

    //获取最近搜索
    public void getZuiJin() {
        setZuiJin();
    }

    //获取常用
    public void getChangYong() {
        setChangYong();
    }

    //设置最近搜索
    public void setZuiJin() {
        zuijinList = new ArrayList();
        zuijinList.add(new ZuiJinEntity());
        zuijinList.add(new ZuiJinEntity());
        zuijinList.add(new ZuiJinEntity());
        zuijinList.add(new ZuiJinEntity());
        zuijinList.add(new ZuiJinEntity());
        zuijinList.add(new ZuiJinEntity());
        zuijinList.add(new ZuiJinEntity());
        zuijinList.add(new ZuiJinEntity());

        zjFlowLayout.setAdapter(new TagAdapter<ZuiJinEntity>(zuijinList) {
            @Override
            public View getView(FlowLayout parent, int position, ZuiJinEntity zuiJinEntity) {
                View inflate = getLayoutInflater().inflate(R.layout.search_item_t_yuan, zjFlowLayout, false);
                TextView viewById = inflate.findViewById(R.id.text);
                viewById.setText(new Random().nextInt(10000000) + "");
                return inflate;
            }
        });

        zjFlowLayout.setOnTagClickListener((view, position, parent) -> {
            TagView tagView = (TagView) view;
//                tagView.setVisibility(View.GONE);
            tagView.setChecked(false);
            goSeachContentActivity(position);
            return true;
        });

    }

    //设置发现
    public void setFaXian() {
        faxianList = new ArrayList();
        faxianList.add(new FaXianEntity());
        faxianList.add(new FaXianEntity());
        faxianList.add(new FaXianEntity());
        faxianList.add(new FaXianEntity());
        faxianList.add(new FaXianEntity());
        faxianList.add(new FaXianEntity());
        faxianList.add(new FaXianEntity());
        faxianList.add(new FaXianEntity());
        fxFlowLayout.setAdapter(new TagAdapter<FaXianEntity>(faxianList) {
            @Override
            public View getView(FlowLayout parent, int position, FaXianEntity faXianEntity) {
                View inflate = getLayoutInflater().inflate(R.layout.search_item_t_yuan, zjFlowLayout, false);
                TextView viewById = inflate.findViewById(R.id.text);
                viewById.setText(new Random().nextInt(100000) + "");
                return inflate;
            }
        });

        fxFlowLayout.setOnTagClickListener((view, position, parent) -> {
            TagView tagView = (TagView) view;
//                tagView.setVisibility(View.GONE);
            tagView.setChecked(false);
            goSeachContentActivity(position);
            return true;
        });

    }

    //设置常用
    public void setChangYong() {
        changyongList = new ArrayList();
        changyongList.add(new ChangYongEntity());
        changyongList.add(new ChangYongEntity());
        changyongList.add(new ChangYongEntity());
        changyongList.add(new ChangYongEntity());
        changyongList.add(new ChangYongEntity());
        changyongList.add(new ChangYongEntity());
        changyongList.add(new ChangYongEntity());
        cyFlowLayout.setAdapter(new TagAdapter<ChangYongEntity>(changyongList) {
            @Override
            public View getView(FlowLayout parent, int position, ChangYongEntity changYongEntity) {
                View inflate = getLayoutInflater().inflate(R.layout.search_item_yuan, zjFlowLayout, false);
                TextView viewById = inflate.findViewById(R.id.text);
                viewById.setText(new Random().nextInt(1000) + "");
                return inflate;
            }
        });

        cyFlowLayout.setOnTagClickListener((view, position, parent) -> {
            TagView tagView = (TagView) view;
            tagView.setChecked(false);
            goSeachContentActivity(position);
            return true;
        });
    }

    //跳转查询商品
    private void goSeachContentActivity(int position) {
        ToolsKit.goDh(this, SearchContentActivity.class);
    }

    private void closeKeyBoard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }
}
